<?

//Initiate Authenticate
SecurityRedirect ();


//initiate Vars 
define("PRIMARY_KEY","project_id");
$FileName       = "ais_setup_project.php";
$FileTemplate   = "ais_setup_project_form.html";
$SubHeaderTitle = "Project";
$TableName		= "tblproject";
$HTMLFormName   = "ProjectForm";

$FormAction    = $FileName . "?" . AddParam(GetQueryString("QueryString", ""),"form","$HTMLFormName");
$FormEncType   = "application/x-www-form-urlencoded";
//$PMode         = "";


//Initiate Database Table For Insert & Update;

//$VerifyFields  = array("ktrasl"=>"Office Code","ktrnam"=>"Office Name");
$FormFields    = array("project_id"=>"","project_name"=>"","customer"=>"","start_date"=>"date","end_date"=>"date","project_location"=>"","project_remarks"=>"","project_owner"=>"");
buildsqlfield ("$TableName", $FormFields);


//Leave Here Untouch
$FormCName     = BuildHTMLField($FormFields,"name");
$FormCValue    = BuildHTMLField($FormFields,"value");
$FormHName     = BuildHTMLField($FormFields,"name",1);
$FormHValue    = BuildHTMLField($FormFields,"value",1);


list($FDay,$FMonth,$FYear) = explode("/",GetParam("start_date",""));
	$start_date = "$FYear-$FMonth-$FDay";
	
list($FDay2,$FMonth2,$FYear2) = explode("/",GetParam("end_date",""));
	$end_date = "$FYear2-$FMonth2-$FDay2";


//************ FORM ACTION **************
if (GetParam("form","")==$HTMLFormName) {
	if (GetParam("Action","")=="Add") {
		$FormMode="Add";
		FormInsert();
	}
	if (GetParam("Action","")=="Update") {
		$FormMode="Edit";
		FormUpdate();
	}
	if (GetParam("Action","")=="Delete") {
		$FormMode="Edit";
		FormDelete();
	}
	if (GetParam("Action","")=="Cancel") {
		$FormMode="Edit";
		FormCancel();
	}
}

//Field Verification 
$Verification  = "Enable";
$SessionVar    = md5(microtime());
//$SessionVar    = "Stuck";


//************ SET FORM MODE ***************

if (GetParam(PRIMARY_KEY,"")) $FormMode = "Edit";
else $FormMode = "Add";

Init($SessionVar,CreateVerifyLoader ($FileName,"iLoader",PRIMARY_KEY,"$HTMLFormName.".PRIMARY_KEY, $FormMode));
$InitFunction = "isExist".PRIMARY_KEY."()";


//******* DATABASE FORM QUERY ***********
$SQL = "SELECT * FROM `$TableName`";
if (GetParam(PRIMARY_KEY,"") && $FormMode="Edit") {
    $SQLForm = $SQL." WHERE ".PRIMARY_KEY."='".GetParam(PRIMARY_KEY,"")."'";
}



//Select Department 
//$SQLDept = "SELECT * FROM `$TableName`";


//Edited by Adit van Smith
$SQLCustomer = "SELECT * FROM ".$DBGeneral.".tbldebitur ORDER BY CUNAME ASC";
//End Edited by Adit van Smith


//******* SHOW FORM CONTENT *********
$TBS = new clsTinyButStrong ;
$TBS->LoadTemplate($MOD_TEMPLATE_DIR."/".$FileTemplate);


if (GetParam(PRIMARY_KEY,"")) $TBS->MergeBlock("blv","mysql",$SQLForm);
else $TBS->MergeBlock("blv",$FormHValue);

//HTML Name
$TBS->MergeBlock("bln",$FormCName);
//HTMLGrid
$TBS->MergeBlock("blg",$DBMysql,$SQL);

$TBS->MergeBlock("customer",$DBMysql,$SQLCustomer);

$TBS->Show();


//*************** ACTION FUNCTION *******************
function FormInsert() {
	global $DBConnection,$FileName;
	$DBConnection->Insert();
	header("Location: $FileName?PageMode=Form");
	exit;
}

function FormUpdate() {
	global $DBConnection;
	$DBConnection->Update();
}

function FormDelete() {
	global $DBConnection,$FileName,$TableName;
	$DBConnection->Delete("$TableName",PRIMARY_KEY);
	header("Location: $FileName");
	exit;	
}

function FormSearch () {

}

function FormCancel () {
	global $FileName;
	header("Location: ".$FileName);
	exit;
}

/*
//*************** USER FUNCTION *******************
function buildsqlfield ($table, $arrayfield) {
		global $DBConnection;
		for ($x=0;$x<count($arrayfield);$x++) {
			$DBConnection->add_htmlfield($table,$arrayfield[$x],GetParam($arrayfield[$x],""));
		}
}*/

//*************** USER FUNCTION *******************

function buildsqlfield ($table, $arrayfield) {
		global $DBConnection;
		foreach ($arrayfield as $key=>$Formatting) {
			$value = GetParam($key,"");
			if ($Formatting=="date") {
				list($day,$month,$year) = explode("/",$value);
				$value = "$year-$month-$day";
			} elseif ($Formatting=="number") $value = str_replace(",","",$value);
			$DBConnection->add_htmlfield($table,$key,$value);
		}
}





?>
